package com.etc.service.impl;

import com.etc.dao.UserMapper;
import com.etc.entity.User;
import com.etc.service.UserService;
import com.etc.util.MD5Utile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper dao;

    @Override
    public User login(String userName) {
        return dao.selectByUsername(userName);
    }

    @Override
    public boolean register(User user) {
        String u_passwordMD5 = MD5Utile.EncodeByMD5(user.getPassword());
        user.setPassword(u_passwordMD5);
        int i = dao.insert(user);
        return i>0;
    }

    @Override
    public boolean updateInfo(User updated, String username) {
        int i = dao.updateByUsername(updated, username);
        return i>0;
    }

    @Override
    public boolean updateUser(User updated, String username) {
        int i = dao.updateUser(updated, username);
        return i>0;
    }

    @Override
    public List<User> getAll() {
        return dao.selectAll();
    }

    @Override
    public boolean deleUser(Long id) {
        int i = dao.deleteById(id);
        return i>0;
    }

    @Override
    public boolean updatUserStatus(Long id,String status) {
        int i = dao.updateById(id,status);
        return i>0;
    }

    @Override
    public boolean addUser(User userInfo) {
        String u_passwordMD5 = MD5Utile.EncodeByMD5(userInfo.getPassword());
        userInfo.setPassword(u_passwordMD5);
        int i = dao.addUser(userInfo);
        return i>0;
    }

    @Override
    public List<User> selectUserByStatus(Integer userLevel) {
        return dao.selectUserByStatus(userLevel);
    }

}
